//
//  XMDynamicTableViewCell.m
//  XunMi
//
//  Created by MacBook on 2024/3/6.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMGuanXuanTableViewCell.h"
#import "XMGuanXuanModel.h"
#import "XMCollectionFlowLayout.h"
#import "XMGuanXuanCollectionViewCell.h"
#import <YBImageBrowser/YBImageBrowser.h>

@interface XMGuanXuanTableViewCell () <XMCollectionFlowLayoutDataSource, UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) XMCollectionFlowLayout *layout;

@property (weak, nonatomic) IBOutlet UIImageView *headImgV;
@property (weak, nonatomic) IBOutlet UILabel *nameLa;
@property (weak, nonatomic) IBOutlet UILabel *timeLa;
@property (weak, nonatomic) IBOutlet UILabel *xuanLa;
@property (weak, nonatomic) IBOutlet UILabel *contentLa;
@property (weak, nonatomic) IBOutlet UIButton *moreBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentH;

@property (weak, nonatomic) IBOutlet UIView *imgBgV;
@property (weak, nonatomic) IBOutlet UIButton *shareBtn;
@property (weak, nonatomic) IBOutlet UIButton *zanBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imgVH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *moreBtnH;
@property (weak, nonatomic) IBOutlet UILabel *omgCountLa;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation XMGuanXuanTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    kCornerRadius(_xuanLa, 11, 1, kAppMainColor);
    [_headImgV radiusWithRadius:20 corner:UIRectCornerAllCorners];

    [_shareBtn setImagePosition:XMImagePositionLeft spacing:6];
    [_zanBtn setImagePosition:XMImagePositionLeft spacing:6];
    
    [self.collectionView setCollectionViewLayout:self.layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:kNibWithName(kStringFromClass(XMGuanXuanCollectionViewCell)) forCellWithReuseIdentifier:kStringFromClass(XMGuanXuanCollectionViewCell)];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHeadImg)];
    [_headImgV addGestureRecognizer:tap];
}

- (void)setModel:(XMGuanXuanModel *)model {
    _model = model;
    self.moreBtn.selected = model.isOpening;
    self.moreBtn.hidden = !model.showMore;
    self.contentLa.text = model.content;
    self.contentH.constant = model.contentH;
    
    self.imgVH.constant = model.imageViewH;
    self.moreBtnH.constant = model.showMore ? 20 : 0;
    
    self.imgBgV.hidden = model.imgs.count <= 0;
    self.omgCountLa.hidden = model.imgs.count <= 3;
    
    [self.collectionView reloadData];
}

- (IBAction)forkAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (self.delegate && [self.delegate respondsToSelector:@selector(handelfFollow:followButton:)]) {
        [self.delegate handelfFollow:self.model followButton:sender];
    }
}

- (IBAction)moreAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (self.delegate && [self.delegate respondsToSelector:@selector(handelMore:cell:)]) {
        [self.delegate handelMore:self.model cell:self];
    }
}

- (IBAction)shareAct:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(handelfShare:shareButton:)]) {
        [self.delegate handelfShare:self.model shareButton:sender];
    }
}

- (IBAction)praiseAct:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(handelPraise:praiseButton:)]) {
        [self.delegate handelPraise:self.model praiseButton:sender];
    }
}

- (void)tapHeadImg {
    if (self.delegate && [self.delegate respondsToSelector:@selector(handelUserDetail:)]) {
        [self.delegate handelUserDetail:self.model];
    }
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.model.imgs.count < 4) {
        return self.model.imgs.count;
    } else {
        return 3;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMGuanXuanCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kStringFromClass(XMGuanXuanCollectionViewCell) forIndexPath:indexPath];
    [cell.imgV sd_setImageWithURL:kStringToUrl(self.model.imgs[indexPath.item]) placeholderImage:kDefaultCoverImg];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableArray *datas = [NSMutableArray array];
    XMGuanXuanCollectionViewCell *cell = (XMGuanXuanCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [self.model.imgs enumerateObjectsUsingBlock:^(NSString *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        // 网络图片
        YBIBImageData *data = [YBIBImageData new];
        data.imageURL = [NSURL URLWithString:obj];
        data.projectiveView = cell.imgV;
        [datas addObject:data];
        
    }];
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSourceArray = datas;
    browser.currentPage = indexPath.item;
    [browser show];

}

#pragma mark - XMCollectionFlowLayoutD
// 一行显示N个
- (NSInteger)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout numberOfColumnInSection:(NSInteger)section {
    if (self.model.imgs.count < 4) {
        return self.model.imgs.count;
    } else {
        return 3;
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout itemWidth:(CGFloat)width heightForItemAtIndexPath:(NSIndexPath *)indexPath {
    return self.model.imageViewH;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout referenceHeightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout referenceHeightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (XMCollectionFlowLayout *)layout {
    if (_layout == nil) {
        _layout = [[XMCollectionFlowLayout alloc] init];
        _layout.dataSource = self;
    }
    return _layout;
}


@end
